package com.naiterui.ehp.bs.weixin.handler;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.weixin.service.WechatService;
import java.util.Map;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.springframework.stereotype.Component;

/**
 * @author gaoll gll910802@163.com
 */
@Slf4j
@Component
@AllArgsConstructor
public class ScanHandler extends AbstractHandler {

    private final WechatService wechatService;

    @Override
    public WxMpXmlOutMessage handle(WxMpXmlMessage wxMpXmlMessage, Map<String, Object> map,
        WxMpService wxMpService, WxSessionManager wxSessionManager) throws WxErrorException {

        logger.info("ScanHandler , wxMessage={}", wxMpXmlMessage);
        String eventKey = wxMpXmlMessage.getEventKey();
        String openId = wxMpXmlMessage.getFromUser();

        try {
            wechatService.scan(openId, eventKey, wxMpService);
        } catch (BusinessException e) {
            e.printStackTrace();
        }
        return null;
    }

}
